﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YuuzleComic.Common;
using System.Data;
using Newtonsoft.Json.Linq;
using YuuzleComic.DBUtility;
using System.Text;
using YuuzleComic.Model;
using YuuzleComic.Web.UI;

public partial class admin_makehtml : YuuzleComic.Web.UI.ManagePage
{
    YuuzleComic.BLL.zz zzbll = new YuuzleComic.BLL.zz();
    YuuzleComic.BLL.comics comicbll = new YuuzleComic.BLL.comics();
    YuuzleComic.BLL.zj zjbll = new YuuzleComic.BLL.zj();
    YuuzleComic.BLL.ycclass classbll = new YuuzleComic.BLL.ycclass();
    YuuzleComic.BLL.diqu diqubll = new YuuzleComic.BLL.diqu();
    protected string strJSON = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["a"] != null)
            {
                switch (Request["a"])
                {
                    case "author":
                        this.makeHtml_Author();
                        break;
                    case "abcd":
                        this.makeHtml_ABCD();
                        break;
                    case "class":
                        this.makeHtml_Class(1);
                        break;
                    case "diqu":
                        this.makeHtml_Class(2);
                        break;
                    case "zj":
                        this.makeHtml_ZJ();
                        break;
                    default:
                        break;
                }
            }
        }
    }

    private void makeHtml_ZJ()
    {
        JObject result = new JObject();
        try
        {
            DataRow dr = DbHelperSQL.Query("select a.*,b.mhmc,b.mhfm,c.zzmc from zj a join comics b on a.cid=b.id join zz c on b.zzid=c.id where a.id=" + Request["id"]).Tables[0].Rows[0];
            ComicInfo cInfo = new ComicInfo();
            cInfo.bid = Convert.ToInt32(dr["cid"]);
            cInfo.bname = dr["mhmc"].ToString();
            cInfo.burl = "/comic/" + dr["cid"] + "/" + dr["id"] + "/";
            cInfo.bpic = dr["mhfm"].ToString();
            cInfo.cid = Convert.ToInt32(dr["id"]);
            cInfo.cname = dr["zjmc"].ToString();
            cInfo.files = new List<string>();
            if (System.IO.Directory.Exists("E:/comicdata/" + dr["tpadd"]))
            {
                cInfo.path = "/comicdata1/" + dr["tpadd"];
            }
            else
            {
                cInfo.path = "/comicdata/" + dr["tpadd"];
            }
            string pageSelect = "";
            string[] tpurlArr = dr["tpurl"].ToString().Split('|');
            for (int i = 0; i < tpurlArr.Length; i++)
            {
                cInfo.files.Add(tpurlArr[i].Split('/')[tpurlArr[i].Split('/').Length - 1]);
                pageSelect += "<option value=\"" + (i + 1) + "\">第" + (i + 1) + "页</option>";
            }
            cInfo.len = cInfo.files.Count;
            ECMAScriptPacker packer = new ECMAScriptPacker();
            #region 替换标签
            string html = Input.ReadTextFile("~/admin/temp/show_1.htm");
            html = Input.CommonReplace(html);
            html = html.Replace("[WEB_NAME]", "雨泽漫画");
            html = html.Replace("[WEB_URL]", "http://www.yzmanga.com/");
            html = html.Replace("[TITLE]", cInfo.bname);
            html = html.Replace("[ZJNAME]", cInfo.cname);
            html = html.Replace("[ZZNAME]", dr["zzmc"].ToString());
            html = html.Replace("[CID]", cInfo.bid.ToString());
            html = html.Replace("[TITLE]", cInfo.bname);
            html = html.Replace("[pageSelect]", pageSelect);
            html = html.Replace("[cInfoInit]", packer.Pack("var cInfo=" + JObject.FromObject(cInfo).ToString() + " || {};"));
            html = html.Replace("[pageCount]", cInfo.len.ToString());
            #endregion
            #region 创建文件
            if (!System.IO.Directory.Exists(Server.MapPath("~/comic/" + cInfo.bid + "/" + cInfo.cid)))//如果不存在就创建文件夹
            {
                System.IO.Directory.CreateDirectory(Server.MapPath("~/comic/" + cInfo.bid + "/" + cInfo.cid));
            }
            Input.WriteTextFile("~/comic/" + cInfo.bid + "/" + cInfo.cid + "/index.htm", html);
            #endregion
            result.Add("s", true);
        }
        catch (Exception e)
        {
            result.Add("s", false);
            result.Add("m", e.Message);
        }
        strJSON = result.ToString();
    }

    private void makeHtml_Class(int type)
    {
        if (true)
        {
            string dic_name, title;
            string sql = "select a.id,a.mhmc,a.mhfm,a.zt,a.adddate comicdate,a.hits,a.tj,c.id zjid,c.zjmc,c.time from comics a ";
            sql += "left join (select MAX(id) zjid,cid from zj group by cid) b on a.id=b.cid left join zj c on b.zjid=c.id ";
            if (type == 1)
            {
                YuuzleComic.Model.ycclass cls = classbll.GetModel(Convert.ToInt32(Request["classid"]));
                dic_name = cls.filename;
                title = cls.classname;
                sql += "where a.classid=" + cls.id;
            }
            else
            {
                YuuzleComic.Model.ycdiqu diqu = diqubll.GetModel(Convert.ToInt32(Request["classid"]));
                dic_name = diqu.filename;
                title = diqu.diqu;
                if (diqu.id == 3 || diqu.id == 5)
                {
                    sql += "where a.dqid not in (1,2,4)";
                }
                else
                {
                    sql += "where a.dqid=" + diqu.id;
                }
            }
            DataTable dt = DbHelperSQL.Query(sql).Tables[0];
            string[] headList = new string[3];
            for (int i = 1; i <= 15; i++)
            {
                DataRow dr = dt.Select("1=1", "hits desc")[i - 1];
                headList[0] += "<li><span class=\"t" + i + "\">" + i + ".</span><a href=\"/comic/" + dr["id"] + "/\" target=\"_blank\" title=\"" + dr["mhmc"] + "\">" + dr["mhmc"] + "</a></li>";
            }
            headList[1] += "<ul style=\"display: block;\">";
            for (int i = 0; i < 8; i++)
            {
                DataRow dr = dt.Select("zt=0", "hits desc")[i];
                headList[1] += "<li><a href=\"/comic/" + dr["id"] + "/\" target=\"_blank\" title=\"" + dr["mhmc"] + "\"><img src=\"/cpic/" + dr["mhfm"] + "\" alt=\"" + dr["mhmc"] + "\" /></a><a href=\"/comic/" + dr["id"] + "/\" target=\"_blank\" title=\"" + dr["mhmc"] + "\">" + dr["mhmc"] + "</a>[<a class=\"red\" href=\"/comic/" + dr["id"] + "/" + dr["zjid"] + "/\" target=\"_blank\" title=\"" + dr["zjmc"] + "\">" + dr["zjmc"] + "</a>]</li>";
            }
            headList[1] += "</ul><ul>";
            for (int i = 0; i < 8; i++)
            {
                DataRow dr = dt.Select("zt=1", "hits desc")[i];
                headList[1] += "<li><a href=\"/comic/" + dr["id"] + "/\" target=\"_blank\" title=\"" + dr["mhmc"] + "\"><img src=\"/cpic/" + dr["mhfm"] + "\" alt=\"" + dr["mhmc"] + "\" /></a><a href=\"/comic/" + dr["id"] + "/\" target=\"_blank\" title=\"" + dr["mhmc"] + "\">" + dr["mhmc"] + "</a>[<a class=\"green\" href=\"/comic/" + dr["id"] + "/" + dr["zjid"] + "/\" target=\"_blank\" title=\"" + dr["zjmc"] + "\">完结</a>]</li>";
            }
            headList[1] += "</ul>";
            for (int i = 0; i < 15; i++)
            {
                DataRow dr = dt.Select("1=1", "adddate desc")[i];
                int days = DateTime.Now.Subtract(Convert.ToDateTime(dr["time"])).Days;
                headList[2] += "<li><span" + (days <= 2 ? " class=\"red\"" : "") + ">" + shortDate(dr["adddate"]) + "</span><a href=\"/comic/" + dr["id"] + "/\" target=\"_blank\" title=\"Six Trigger\">" + dr["mhmc"] + "</a>[";
                if (Convert.ToBoolean(dr["zt"]))
                {
                    headList[2] += "<a class=\"green\" href=\"/comic/" + dr["id"] + "/" + dr["zjid"] + "/\" target=\"_blank\" title=\"完结\">完结</a>";
                }
                else
                {
                    headList[2] += "<a class=\"red\" href=\"/comic/" + dr["id"] + "/" + dr["zjid"] + "/\" target=\"_blank\" title=\"" + dr["zjmc"] + "\">" + dr["zjmc"] + "</a>";
                }
                headList[2] += "]</li>";
            }
            this.makeHtml_Class_Page(dt.Select("1=1", "comicdate desc"), title, dic_name, "index", headList);
            this.makeHtml_Class_Page(dt.Select("1=1", "adddate desc"), title, dic_name, "update", headList);
            this.makeHtml_Class_Page(dt.Select("1=1", "hits desc"), title, dic_name, "view", headList);
        }
    }

    private void makeHtml_Class_Page(DataRow[] drArr, string title, string dic_name, string page_name, string[] headList)
    {
        int pageSize = 48;
        string list = "";
        int pageCount = (drArr.Length + pageSize - 1) / pageSize;
        int currentPage = 0;
        for (int i = 0; i < drArr.Length; i++)
        {
            DataRow dr = drArr[i];
            #region 生成列表内容
            list += "<li><a title=\"" + dr["mhmc"] + "\" href=\"/comic/" + dr["id"] + "/\" target=\"_blank\">";
            list += "<img alt=\"" + dr["mhmc"] + "\" src=\"/cpic/" + dr["mhfm"] + "\" />" + dr["mhmc"] + "</a><em>「 ";
            if (Convert.ToBoolean(dr["zt"]))
            {
                list += "<a class=\"green\" href=\"/comic/" + dr["id"] + "/" + dr["zjid"] + "/\" target=\"_blank\" title=\"最近更新时间：" + shortDate(dr["time"]) + "\">已完结</a>";
            }
            else
            {
                list += "更新到：<a class=\"red\" href=\"/comic/" + dr["id"] + "/" + dr["zjid"] + "/\" target=\"_blank\" title=\"最近更新时间:" + shortDate(dr["time"]) + "\">" + dr["zjmc"] + "</a>";
            }
            list += " 」</em></li>";
            #endregion
            if ((i + 1) % pageSize == 0 || i + 1 == drArr.Length)
            {
                currentPage++;
                #region 页头分页
                string pageHead = "<a href=\"" + page_name + ".htm#best\" class=\"first\" title=\"首页\"><em>首页</em></a>";
                pageHead += "<a href=\"" + page_name + (currentPage - 1 <= 1 ? "" : "_p" + (currentPage - 1)) + ".htm#best\" class=\"prev\" title=\"上一页\"><em>上一页</em></a>";
                pageHead += "<a href=\"" + page_name + "_p" + (currentPage + 1 >= pageCount ? pageCount : (currentPage + 1)) + ".htm#best\" class=\"next\" title=\"下一页\"><em>下一页</em></a>";
                pageHead += "<a href=\"" + page_name + (pageCount <= 1 ? "" : "_p" + pageCount) + ".htm#best\" class=\"last\" title=\"尾页\"><em>尾页</em></a>";
                #endregion
                #region 页尾分页
                string pageFoot = currentPage != 1 ? "&nbsp;<a href=\"" + page_name + ".htm#best\" title=\"首页\">首页</a>" : "";
                int s = currentPage - 4;
                if (s < 1)
                {
                    s = 1;
                }
                for (int f = s; f <= pageCount; f++)
                {
                    if (currentPage == f)
                    {
                        pageFoot += "&nbsp;<span>[" + f + "]</span>";
                    }
                    else if (f == 1)
                    {
                        pageFoot += "&nbsp;<a href=\"" + page_name + ".htm#best\" title=\"第1页\">[1]</a>";
                    }
                    else
                    {

                        pageFoot += "&nbsp;<a href=\"" + page_name + "_p" + (f > pageCount ? pageCount : f) + ".htm#best\" title=\"第" + f + "页\">[" + f + "]</a>";
                    }
                    if (f >= s + 8)
                    {
                        break;
                    }
                }
                if (currentPage != pageCount)
                {
                    pageFoot += "&nbsp;<a href=\"" + page_name + "_p" + pageCount + ".htm#best\" title=\"尾页\">尾页</a>";
                }
                #endregion
                #region 替换标签
                string html = Input.ReadTextFile("~/admin/temp/type.htm");
                html = Input.CommonReplace(html);
                html = html.Replace("[WEB_NAME]", "雨泽漫画");
                html = html.Replace("[WEB_URL]", "http://www.yzmanga.com/");
                html = html.Replace("[TITLE]", title);
                html = html.Replace("[LIST]", list);
                html = html.Replace("[PAGE]", currentPage.ToString());
                html = html.Replace("[PAGE_COUNT]", pageCount.ToString());
                html = html.Replace("[COUNT]", drArr.Length.ToString());
                html = html.Replace("[ABCD]", dic_name);
                html = html.Replace("[PAGE_HEAD]", pageHead);
                html = html.Replace("[PAGE_FOOT]", pageFoot);
                html = html.Replace("[o1]", page_name == "index" ? " class=\"on\"" : "");
                html = html.Replace("[o2]", page_name == "update" ? " class=\"on\"" : "");
                html = html.Replace("[o3]", page_name == "view" ? " class=\"on\"" : "");
                html = html.Replace("[topHits]", headList[0]);
                html = html.Replace("[comicList]", headList[1]);
                html = html.Replace("[newUpdate]", headList[2]);
                #endregion
                #region 创建文件
                if (!System.IO.Directory.Exists(Server.MapPath("~/list/" + dic_name)))//如果不存在就创建文件夹
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath("~/list/" + dic_name));
                }
                Input.WriteTextFile("~/list/" + dic_name + "/" + page_name + (currentPage == 1 ? "" : "_p" + currentPage) + ".htm", html);
                #endregion
                list = "";
            }
        }
    }

    private void makeHtml_ABCD()
    {
        if (Request.QueryString["abcd"] != null)
        {
            string abcd = Request.QueryString["abcd"];
            string title = "字母索引 " + abcd;
            string sql = "select a.id,a.mhmc,a.mhfm,a.zt,a.adddate comicdate,a.hits,a.zzid,a.tj,left(a.mhjj,45)+'……' as mhjs,c.id zjid,c.zjmc,c.adddate,d.zzmc from comics a ";
            sql += "left join (select MAX(id) zjid,cid from zj group by cid) b on a.id=b.cid left join zj c on b.zjid=c.id left join zz d on a.zzid=d.id ";
            if (abcd == "0-9")
            {
                sql += "where a.zm in ('0','1','2','3','4','5','6','7','8','9','0-9')";
            }
            else
            {
                sql += "where a.zm='" + abcd + "'";
            }
            DataTable dt = DbHelperSQL.Query(sql).Tables[0];
            this.makeHtml_ABCD_Page(dt.Select("1=1", "comicdate desc"), abcd, "index");
            this.makeHtml_ABCD_Page(dt.Select("1=1", "adddate desc"), abcd, "update");
            this.makeHtml_ABCD_Page(dt.Select("1=1", "hits desc"), abcd, "view");
        }
    }

    private void makeHtml_ABCD_Page(DataRow[] drArr, string abcd, string name)
    {
        int pageSize = 30;
        string list = "";
        int pageCount = (drArr.Length + pageSize - 1) / pageSize;
        int currentPage = 0;
        for (int i = 0; i < drArr.Length; i++)
        {
            DataRow dr = drArr[i];
            #region 生成列表内容
            list += "<li><div class=\"cover\"><a href=\"/comic/" + dr["id"] + "/\"><img src=\"/cpic/" + dr["mhfm"] + "\" alt=\"" + dr["mhmc"] + "\" /></a></div>";
            list += "<div class=\"intro\"><h2><a href=\"/comic/" + dr["id"] + "/\" title=\"" + dr["mhmc"] + "\">" + dr["mhmc"] + "</a></h2><em>[ ";
            if (Convert.ToBoolean(dr["zt"]))
            {
                list += "<a class=\"green\" target=\"_blank\" href=\"/comic/" + dr["id"] + "/" + dr["zjid"] + "/\" title=\"完结\">完结</a>";
            }
            else
            {
                list += "最新话卷：<a class=\"red\" target=\"_blank\" href=\"/comic/" + dr["id"] + "/" + dr["zjid"] + "/\" title=\"最近更新时间:" + shortDate(dr["time"]) + "\">" + dr["zjmc"] + "</a>";
            }
            list += " ]</em><em> [ 最近更新：<abbr>" + shortDate(dr["time"]) + "</abbr> ";
            list += "]</em><span>" + dr["mhjs"] + "</span><em>[ ";
            list += "作者：<a href=\"/list/author/" + dr["zzid"] + ".htm\">" + dr["zzmc"] + "</a> ]</em></div></li>";
            #endregion
            if ((i + 1) % pageSize == 0 || i + 1 == drArr.Length)
            {
                currentPage++;
                #region 页头分页
                string pageHead = "<a href=\"" + name + ".htm#best\" class=\"first\" title=\"首页\"><em>首页</em></a>";
                pageHead += "<a href=\"" + name + (currentPage - 1 <= 1 ? "" : "_p" + (currentPage - 1)) + ".htm#best\" class=\"prev\" title=\"上一页\"><em>上一页</em></a>";
                pageHead += "<a href=\"" + name + "_p" + (currentPage + 1 >= pageCount ? pageCount : (currentPage + 1)) + ".htm#best\" class=\"next\" title=\"下一页\"><em>下一页</em></a>";
                pageHead += "<a href=\"" + name + (pageCount <= 1 ? "" : "_p" + pageCount) + ".htm#best\" class=\"last\" title=\"尾页\"><em>尾页</em></a>";
                #endregion
                #region 页尾分页
                string pageFoot = currentPage != 1 ? "&nbsp;<a href=\"" + name + ".htm#best\" title=\"首页\">首页</a>" : "";
                int s = currentPage - 4;
                if (s < 1)
                {
                    s = 1;
                }
                for (int f = s; f <= pageCount; f++)
                {
                    if (currentPage == f)
                    {
                        pageFoot += "&nbsp;<span>[" + f + "]</span>";
                    }
                    else if (f == 1)
                    {
                        pageFoot += "&nbsp;<a href=\"" + name + ".htm#best\" title=\"第1页\">[1]</a>";
                    }
                    else
                    {

                        pageFoot += "&nbsp;<a href=\"" + name + "_p" + (f > pageCount ? pageCount : f) + ".htm#best\" title=\"第" + f + "页\">[" + f + "]</a>";
                    }
                    if (f >= s + 8)
                    {
                        break;
                    }
                }
                if (currentPage != pageCount)
                {
                    pageFoot += "&nbsp;<a href=\"" + name + "_p" + pageCount + ".htm#best\" title=\"尾页\">尾页</a>";
                }
                #endregion
                #region 替换标签
                string html = Input.ReadTextFile("~/admin/temp/abcd.htm");
                html = Input.CommonReplace(html);
                html = html.Replace("[WEB_NAME]", "雨泽漫画");
                html = html.Replace("[WEB_URL]", "http://www.yzmanga.com/");
                html = html.Replace("[TITLE]", "字母索引 " + abcd);
                html = html.Replace("[LIST]", list);
                html = html.Replace("[PAGE]", currentPage.ToString());
                html = html.Replace("[PAGE_COUNT]", pageCount.ToString());
                html = html.Replace("[COUNT]", drArr.Length.ToString());
                html = html.Replace("[ABCD]", abcd);
                html = html.Replace("[PAGE_HEAD]", pageHead);
                html = html.Replace("[PAGE_FOOT]", pageFoot);
                html = html.Replace("[o1]", name == "index" ? " class=\"on\"" : "");
                html = html.Replace("[o2]", name == "update" ? " class=\"on\"" : "");
                html = html.Replace("[o3]", name == "view" ? " class=\"on\"" : "");
                #endregion
                #region 创建文件
                if (!System.IO.Directory.Exists(Server.MapPath("~/list/" + abcd)))//如果不存在就创建文件夹
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath("~/list/" + abcd));
                }
                Input.WriteTextFile("~/list/" + abcd + "/" + name + (currentPage == 1 ? "" : "_p" + currentPage) + ".htm", html);
                #endregion
                list = "";
            }
        }
    }

    private void makeHtml_Author()
    {
        try 
	    {
            int zzid = Convert.ToInt32(Request["id"]);
            string html = Input.ReadTextFile("~/admin/temp/author.htm");
            html = Input.CommonReplace(html);

            html = html.Replace("[WEB_NAME]", "雨泽漫画");
            html = html.Replace("[WEB_URL]", "http://www.yzmanga.com/");

            YuuzleComic.Model.zz zz = zzbll.GetModel(zzid);
            DataSet ds = DbHelperSQL.Query("select a.id,a.mhmc,a.mhfm,a.zt,a.tj,left(a.mhjj,45)+'……' as mhjs,c.id zjid,c.zjmc,c.adddate from comics a join (select max(id) mid,cid from zj group by cid) b on a.id=b.cid join zj c on b.mid=c.id where zzid=" + zzid + " order by a.hits desc");
            string list = "";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                bool wanjie = Convert.ToBoolean(dr["zt"]);
                list += "<li><div class=\"cover\"><a href=\"/comic/" + dr["id"] + "/\"><img src=\"/cpic/" + dr["mhfm"] + "\" alt=\"" + dr["mhmc"] + "\" /></a></div>";
                list += "<div class=\"intro\"><h2><a href=\"/comic/" + dr["id"] + "/\" class=\"" + (Convert.ToBoolean(dr["tj"]) ? "new" : "") + "\" title=\"" + dr["mhmc"] + "\">" + dr["mhmc"] + "</a></h2><em>[";
                if (Convert.ToBoolean(dr["zt"]))
                {
                    list += "<a class=\"green\" target=\"_blank\" href=\"/comic/" + dr["id"] + "/" + dr["zjid"] + "/\" title=\"完结\">完结</a>";
                }
                else
                {
                    list += "最新话卷：<a class=\"red\" target=\"_blank\" href=\"/comic/" + dr["id"] + "/" + dr["zjid"] + "/\" title=\"最近更新时间:" + shortDate(dr["time"]) + "\">" + dr["zjmc"] + "</a>";
                }
                list += "]</em><em> [ 最近更新：<abbr>" + shortDate(dr["time"]) + "</abbr>]</em><span>" + dr["mhjs"] + "</span><em>[ 作者：" + zz.zzmc + " ]</em></div></li>";
            }

            html = html.Replace("[ZZMC]", zz.zzmc);
            html = html.Replace("[ZZTP]", zz.zztp);
            html = html.Replace("[ZZJS]", zz.zzjs == "0" ? "暂无作者资料" : zz.zzjs);
            html = html.Replace("[COUNT]", ds.Tables[0].Rows.Count.ToString());
            html = html.Replace("[LIST]", list);

            if (!System.IO.Directory.Exists(Server.MapPath("~/list/author")))//如果不存在就创建文件夹
            {
                System.IO.Directory.CreateDirectory(Server.MapPath("~/list/author"));
            }
            Input.WriteTextFile("~/list/author/" + zzid + ".htm", html);
            strJSON = JObject.Parse("{success:true}").ToString();
	    }
	    catch (Exception e)
	    {
            strJSON = JObject.Parse("{success:false,message:'" + e.Message + "'}").ToString();
	    }
    }
}